from webbrowser import open as _webbrowser_open
from requests import get as _get
__all__ = [
    "TRANSLATE_YOUDAO",
    "SEARCH_BAIDU",
    "UA",
    "open_withbrowser",
    "search",
    "translate",
]
TRANSLATE_YOUDAO = "http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i="
SEARCH_BAIDU = "http://www.baidu.com/s?wd="
UA = {
    "main": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69",
    "简单搜索": "Mozilla/5.0 (Linux; Android 8.0; MI 6 Build/OPR1.170623.027; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.116 Mobile Safari/537.36 T7/10.3 SearchCraft/2.6.3 (Baidu; P1 8.0.0)",
    "微信": "Mozilla/5.0 (Linux; Android 6.0; NEM-AL10 Build/HONORNEM-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/043906 Mobile Safari/537.36 MicroMessenger/6.6.1.1220(0x26060133) NetType/WIFI Language/zh_CN",
    "百度网盘": "netdisk;5.5.1;PC;PC-Windows;6.2.9200;WindowsBaiduYunGuanJia",
    "塞班": "Mozilla/5.0 (Symbian/3; Series60/5.2 NokiaN8-00/012.002; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/533.4 (KHTML, like Gecko) NokiaBrowser/7.3.0 Mobile Safari/533.4 3gpp-gba",
    "IE10.0": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)",
}


def open_withbrowser(fp: str = '') -> None:
    """用浏览器打开本地文件"""
    _webbrowser_open(url=fp)


def search(kw: str = '') -> str:
    """百度搜索"""
    url = SEARCH_BAIDU+kw
    headers = {
        "User-Agent": UA["main"],
    }
    response = _get(url=url, headers=headers)
    return response.text


def translate(kw: str = '') -> str:
    """翻译"""
    response = _get(url=TRANSLATE_YOUDAO+kw)
    return response.json()["translateResult"][0][0]["tgt"]
